<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title th:text="个人资料"></title>
    <link rel="stylesheet" type="text/css" href="/plugins/vali/css/main.css">
    <link rel="stylesheet" type="text/css" href="/plugins/toast/jquery.toast.css">
    <link rel="stylesheet" type="text/css" href="/plugins/font-awesome-4.7.0/css/font-awesome.min.css">
</head>
<body class="app sidebar-mini">
    <!-- 头部 -->
    <header class="app-header">
        <a class="app-header__logo" href="/user/dashboard">用户中心</a>
        <a class="app-sidebar__toggle" href="#" data-toggle="sidebar" aria-label="Hide Sidebar"></a>
        <ul class="app-nav">
            <li class="dropdown">
                <a class="app-nav__item" href="#" data-toggle="dropdown" aria-label="Open Profile Menu">
                    <i class="fa fa-user fa-lg"></i>
                </a>
                <ul class="dropdown-menu settings-menu dropdown-menu-right">
                    <li><a class="dropdown-item" href="/user/profile"><i class="fa fa-user fa-lg"></i> 个人资料</a></li>
                    <li><a class="dropdown-item" href="/user/logout"><i class="fa fa-sign-out fa-lg"></i> 退出登录</a></li>
                </ul>
            </li>
        </ul>
    </header>
    
    <!-- 侧边栏 -->
    <div class="app-sidebar__overlay" data-toggle="sidebar"></div>
    <aside class="app-sidebar">
        <div class="app-sidebar__user">
            <img class="app-sidebar__user-avatar" 
                 th:src="${session.user_session.userPortrait == null ? '/static/img/portrait.jpg' : session.user_session.userPortrait}" 
                 alt="User Image" width="48px" height="48px">
            <div>
                <p class="app-sidebar__user-name" th:text="${session.user_session.userDisplayName}"></p>
                <p class="app-sidebar__user-designation">博客用户</p>
            </div>
        </div>
        <ul class="app-menu">
            <li>
                <a class="app-menu__item" href="/user/dashboard">
                    <i class="app-menu__icon fa fa-dashboard"></i>
                    <span class="app-menu__label">仪表板</span>
                </a>
            </li>
            <li>
                <a class="app-menu__item" href="/user/articles">
                    <i class="app-menu__icon fa fa-file-text"></i>
                    <span class="app-menu__label">文章管理</span>
                </a>
            </li>
            <li>
                <a class="app-menu__item active" href="/user/profile">
                    <i class="app-menu__icon fa fa-user"></i>
                    <span class="app-menu__label">个人资料</span>
                </a>
            </li>
            <li>
                <a class="app-menu__item" href="/" target="_blank">
                    <i class="app-menu__icon fa fa-home"></i>
                    <span class="app-menu__label">返回主页</span>
                </a>
            </li>
        </ul>
    </aside>
    
    <!-- 主内容区 -->
    <main class="app-content">
        <div class="app-title">
            <div>
                <h1><i class="fa fa-user"></i> 个人资料</h1>
                <p>修改您的个人信息</p>
            </div>
            <ul class="app-breadcrumb breadcrumb">
                <li class="breadcrumb-item"><i class="fa fa-home fa-lg"></i></li>
                <li class="breadcrumb-item"><a href="/user/dashboard">仪表板</a></li>
                <li class="breadcrumb-item">个人资料</li>
            </ul>
        </div>
        
        <div class="row">
            <!-- 个人信息 -->
            <div class="col-md-6">
                <div class="tile">
                    <div class="tile-title-w-btn">
                        <h3 class="title">个人信息</h3>
                    </div>
                    <div class="tile-body">
                        <form id="profileForm">
                            <input type="hidden" name="userId" th:value="${user.userId}">
                            <div class="form-group">
                                <label for="userDisplayName">显示名称</label>
                                <input class="form-control" type="text" id="userDisplayName" name="userDisplayName" 
                                       th:value="${user.userDisplayName}" placeholder="显示名称">
                            </div>
                            <div class="form-group">
                                <label for="userEmail">邮箱</label>
                                <input class="form-control" type="email" id="userEmail" name="userEmail" 
                                       th:value="${user.userEmail}" placeholder="邮箱">
                            </div>
                            <div class="form-group">
                                <label for="userPortrait">头像</label>
                                <div class="input-group">
                                    <input class="form-control" type="text" id="userPortrait" name="userPortrait" 
                                           th:value="${user.userPortrait}" placeholder="头像URL">
                                    <div class="input-group-append">
                                        <button class="btn btn-primary" type="button" onclick="openAttach('userPortrait')">选择</button>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="userExplain">个人说明</label>
                                <textarea class="form-control" id="userExplain" name="userExplain" rows="4" 
                                          placeholder="个人说明" th:text="${user.userExplain}"></textarea>
                            </div>
                            <div class="form-group">
                                <button class="btn btn-primary" type="button" onclick="saveProfile()">
                                    <i class="fa fa-fw fa-lg fa-check-circle"></i>保存
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            
            <!-- 修改密码 -->
            <div class="col-md-6">
                <div class="tile">
                    <div class="tile-title-w-btn">
                        <h3 class="title">修改密码</h3>
                    </div>
                    <div class="tile-body">
                        <form id="passwordForm">
                            <div class="form-group">
                                <label for="formerlyPwd">原密码</label>
                                <input class="form-control" type="password" id="formerlyPwd" name="formerlyPwd" placeholder="原密码">
                            </div>
                            <div class="form-group">
                                <label for="newPwd">新密码</label>
                                <input class="form-control" type="password" id="newPwd" name="newPwd" placeholder="新密码">
                            </div>
                            <div class="form-group">
                                <label for="confirmPwd">确认新密码</label>
                                <input class="form-control" type="password" id="confirmPwd" name="confirmPwd" placeholder="确认新密码">
                            </div>
                            <div class="form-group">
                                <button class="btn btn-primary" type="button" onclick="updatePwd()">
                                    <i class="fa fa-fw fa-lg fa-check-circle"></i>更新密码
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
                
                <!-- 账号信息 -->
                <div class="tile">
                    <div class="tile-title-w-btn">
                        <h3 class="title">账号信息</h3>
                    </div>
                    <div class="tile-body">
                        <p><strong>用户名：</strong> <span th:text="${user.userName}"></span></p>
                        <p><strong>最后登录时间：</strong> <span th:text="${#dates.format(user.loginLastTime, 'yyyy-MM-dd HH:mm:ss')}"></span></p>
                    </div>
                </div>
            </div>
        </div>
    </main>
    
    <!-- 脚本 -->
    <script src="/plugins/vali/js/jquery-3.2.1.min.js"></script>
    <script src="/plugins/vali/js/popper.min.js"></script>
    <script src="/plugins/vali/js/bootstrap.min.js"></script>
    <script src="/plugins/vali/js/main.js"></script>
    <script src="/plugins/toast/jquery.toast.js"></script>
    <script src="/plugins/layer/layer.js"></script>
    <script>
        // 打开附件选择
        function openAttach(id) {
            layer.open({
                type: 2,
                title: '所有附件',
                shadeClose: true,
                shade: 0.5,
                area: ['90%', '90%'],
                maxmin: true,
                content: '/admin/profile/openChoice/' + id,
                scrollbar: false
            });
        }
        
        // 保存个人资料
        function saveProfile() {
            var formData = $("#profileForm").serialize();
            $.ajax({
                type: 'post',
                url: '/user/updateProfile',
                data: formData,
                success: function(data) {
                    if (data.flag === true) {
                        $.toast({
                            text: data.msg,
                            heading: '提示',
                            icon: 'success',
                            showHideTransition: 'fade',
                            allowToastClose: true,
                            hideAfter: 1000,
                            stack: 1,
                            position: 'top-center',
                            textAlign: 'left',
                            loader: true,
                            loaderBg: '#ffffff',
                            afterHidden: function() {
                                window.location.reload();
                            }
                        });
                    } else {
                        showMsg(data.msg, "error", 2000);
                    }
                },
                error: function() {
                    showMsg("保存失败", "error", 2000);
                }
            });
        }
        
        // 修改密码
        function updatePwd() {
            var formerlyPwd = $("#formerlyPwd").val();
            var newPwd = $("#newPwd").val();
            var confirmPwd = $("#confirmPwd").val();
            
            if (!formerlyPwd) {
                showMsg("请输入原密码", "error", 2000);
                return;
            }
            if (!newPwd) {
                showMsg("请输入新密码", "error", 2000);
                return;
            }
            if (!confirmPwd) {
                showMsg("请确认新密码", "error", 2000);
                return;
            }
            if (newPwd !== confirmPwd) {
                showMsg("两次密码不一致", "error", 2000);
                return;
            }
            
            $.ajax({
                type: 'post',
                url: '/user/updatePwd',
                data: {
                    formerlyPwd: formerlyPwd,
                    newPwd: newPwd,
                    confirmPwd: confirmPwd
                },
                success: function(data) {
                    if (data.flag === true) {
                        $.toast({
                            text: data.msg,
                            heading: '提示',
                            icon: 'success',
                            showHideTransition: 'fade',
                            allowToastClose: true,
                            hideAfter: 1000,
                            stack: 1,
                            position: 'top-center',
                            textAlign: 'left',
                            loader: true,
                            loaderBg: '#ffffff',
                            afterHidden: function() {
                                window.location.href = "/user/login";
                            }
                        });
                    } else {
                        showMsg(data.msg, "error", 2000);
                    }
                },
                error: function() {
                    showMsg("修改失败", "error", 2000);
                }
            });
        }
        
        // 显示消息
        function showMsg(msg, type, time) {
            $.toast({
                text: msg,
                heading: '提示',
                icon: type,
                showHideTransition: 'fade',
                allowToastClose: true,
                hideAfter: time,
                stack: 1,
                position: 'top-center',
                textAlign: 'left',
                loader: true,
                loaderBg: '#ffffff'
            });
        }
    </script>
</body>
</html>
